import AMapLoader from "@amap/amap-jsapi-loader";

// 高德地图API密钥
const API_KEY = "您的高德地图API密钥";

// 地图实例类型
export interface MapInstance {
  map: any;
  AMap: any;
  plugins: {
    scale?: any;
    toolBar?: any;
    driving?: any;
    [key: string]: any;
  };
}

// 地图配置选项
export interface MapOptions {
  center?: [number, number]; // 中心点坐标
  zoom?: number; // 缩放级别
  viewMode?: "2D" | "3D"; // 视图模式
  mapStyle?: string; // 地图样式
  showIndoorMap?: boolean; // 是否显示室内地图
  showLabel?: boolean; // 是否显示文字标记
  showBuilding?: boolean; // 是否显示建筑物
}

// 默认地图配置
const defaultMapOptions: MapOptions = {
  center: [104.06, 30.67], // 成都市中心坐标
  zoom: 12,
  viewMode: "3D",
  mapStyle: "amap://styles/normal",
  showIndoorMap: false,
  showLabel: true,
  showBuilding: true,
};

/**
 * 初始化高德地图
 * @param container 地图容器元素
 * @param options 地图配置选项
 * @returns Promise<MapInstance> 地图实例
 */
export async function initMap(
  container: HTMLElement,
  options: MapOptions = {},
): Promise<MapInstance> {
  // 合并默认配置和用户配置
  const mapOptions = { ...defaultMapOptions, ...options };

  try {
    // 加载高德地图API
    const AMap = await AMapLoader.load({
      key: API_KEY,
      version: "2.0",
      plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.Driving", "AMap.MapType"],
    });

    // 创建地图实例
    const map = new AMap.Map(container, {
      viewMode: mapOptions.viewMode,
      zoom: mapOptions.zoom,
      center: mapOptions.center,
      mapStyle: mapOptions.mapStyle,
      showIndoorMap: mapOptions.showIndoorMap,
      showLabel: mapOptions.showLabel,
      showBuilding: mapOptions.showBuilding,
    });

    // 添加比例尺控件
    const scale = new AMap.Scale();
    map.addControl(scale);

    // 添加工具条控件
    const toolBar = new AMap.ToolBar();
    map.addControl(toolBar);

    // 添加地图类型切换控件
    const mapType = new AMap.MapType({
      defaultType: 0, // 默认显示普通地图
    });
    map.addControl(mapType);

    // 创建驾车规划实例
    const driving = new AMap.Driving({
      map: map,
      panel: false,
      policy: AMap.DrivingPolicy.LEAST_TIME, // 最快捷模式
    });

    // 返回地图实例和相关插件
    return {
      map,
      AMap,
      plugins: {
        scale,
        toolBar,
        driving,
        mapType,
      },
    };
  } catch (error) {
    console.error("地图初始化失败:", error);
    throw error;
  }
}

/**
 * 切换地图类型
 * @param map 地图实例
 * @param type 地图类型 'normal' | 'satellite'
 */
export function switchMapType(mapInstance: MapInstance, type: "normal" | "satellite"): void {
  if (!mapInstance || !mapInstance.map) return;

  const { map } = mapInstance;

  if (type === "normal") {
    map.setMapStyle("amap://styles/normal");
  } else if (type === "satellite") {
    map.setMapStyle("amap://styles/satellite");
  }
}

/**
 * 添加标记点
 * @param mapInstance 地图实例
 * @param position 标记点位置
 * @param options 标记点选项
 * @returns 标记点实例
 */
export function addMarker(
  mapInstance: MapInstance,
  position: [number, number],
  options: any = {},
): any {
  if (!mapInstance || !mapInstance.map || !mapInstance.AMap) return null;

  const { map, AMap } = mapInstance;

  const marker = new AMap.Marker({
    position: position,
    title: options.title || "",
    icon: options.icon || "",
    label: options.label || null,
    ...options,
  });

  map.add(marker);
  return marker;
}

/**
 * 规划两点间驾车路线
 * @param mapInstance 地图实例
 * @param start 起点坐标
 * @param end 终点坐标
 */
export function planRoute(
  mapInstance: MapInstance,
  start: [number, number],
  end: [number, number],
): void {
  if (!mapInstance || !mapInstance.plugins.driving) return;

  const { driving } = mapInstance.plugins;

  driving.search(start, end, (status: string, result: any) => {
    if (status === "complete") {
      console.log("规划路线成功");
    } else {
      console.error("规划路线失败:", result);
    }
  });
}

/**
 * 清除地图上的所有覆盖物
 * @param mapInstance 地图实例
 */
export function clearMap(mapInstance: MapInstance): void {
  if (!mapInstance || !mapInstance.map) return;

  mapInstance.map.clearMap();
}

/**
 * 销毁地图实例
 * @param mapInstance 地图实例
 */
export function destroyMap(mapInstance: MapInstance): void {
  if (!mapInstance || !mapInstance.map) return;

  mapInstance.map.destroy();
}
